var ports = require("../../utils/ports.js")
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  pushAmount(e) {
    let that = this
    let amount = e.detail.value.amount
    if (amount <= 0) {
      wx.showToast({
        title: '请输入充值金额！',
        icon: 'none'
      })
      return
    }
    let data = {}
    data.amount = amount
    that.submitOneMemberCharge(data)
  },
  //商品详情创建订单
  submitOneMemberCharge(data) {
    var that = this
    wx.showToast({
      title: '提交中',
      icon: 'loading',
      mask: true
    })
    ports.ModuleMemberOrder.submitOneMemberCharge(data, that, res => {
      if (res.data.header.code == 0) {
        let payData = {}
        payData.memberRechargeID = res.data.body.memberRechargeID;
        payData.openid = wx.getStorageSync("USER_OPENID")
        payData.payWayID = app.wxPaywayID
        payData.body = '会员充值'
        that.appApplyMemberRechargePay(payData)
      } else {
        wx.showToast({
          title: res.data.header.msg,
          icon: 'none'
        })
      }
    })
  },
  //申请订单支付
  appApplyMemberRechargePay(data) {
    var that = this
    ports.ModuleMemberOrder.appApplyMemberRechargePay(data, that, res => {
      if (res.data.header.code == 0) {
        var data = res.data.body;
        wx.requestPayment({
          timeStamp: data.timeStamp,
          nonceStr: data.nonceStr,
          package: data.package,
          signType: data.signType,
          paySign: data.paySign,
          success(res) {
            wx.showToast({
              title: '支付成功',
              icon: 'none',
              mask: true,
              duration: 1000
            })
            setTimeout(() => {
              wx.navigateBack()
            }, 1000);
          },
          fail() {
            wx.showToast({
              title: '取消支付',
              icon: 'none',
              mask: true,
              duration: 1000
            })
            setTimeout(() => {
              wx.navigateBack()
            }, 1000);
          }
        })
      } else {
        wx.showToast({
          title: res.data.header.msg,
          icon: 'none'
        })
      }
    })
  }
})